//
//  ThemeManagerViewController.m
//  DaJiang
//
//  Created by sally on 16/10/6.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "OrderCell.h"
#import "ThemeEditViewController.h"
#import "ThemeManagerViewController.h"
#import "TopicModel.h"

@interface ThemeManagerViewController ()<ThemeEditVcDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (assign, nonatomic) NSInteger pager;

@end

@implementation ThemeManagerViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
  [self setRefresh];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"管理话题";
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
}

- (void)setRefresh {
  __weak typeof(self) weakSelf = self;
  // 添加下拉刷新列表数据功能
  _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadRefreshData)];

  // 添加上拉加载更多列表数据功能
  _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadMoreData)];

  // 触发刷新
  [_tableView.header beginRefreshing];
}

#pragma mark - 获取数据
- (void)loadRefreshData {
  _pager = 1;
  [self loadData];
}

- (void)loadMoreData {
  _pager = _pager + 1;
  [self loadData];
}

- (void)loadData {
  self.tableView.footer.hidden = NO;
  NSInteger cursor = _pager > 1 ? [[self.tableData lastObject] topicid] : GCursorInit;

  [TopicModel queryTopicListWithCursor:cursor completion:^(NSMutableArray *topicList) {
    if (topicList) {
      // 下拉刷新数据
      if (_pager == 1) {
        [self.tableData removeAllObjects];
        [self.tableData addObjectsFromArray:topicList];
        [self.tableView reloadData];

        if (topicList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }
      // 上拉加载更多
      else {
        [self.tableData addObjectsFromArray:topicList];
        [self.tableView reloadData];
        if (topicList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }

    } else {
      NSError *error = [TopicModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }

    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
  }];
}

#pragma mark - action
#pragma mark 点击添加话题
- (IBAction)actionOnAdd:(id)sender {
  ThemeEditViewController *vc = [[ThemeEditViewController alloc] init];
  vc.themeEditType = ThemeEditType_Add;
  vc.delegate = self;
  [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  return cell.frame.size.height - GNormalViewHeight * 2 - GNormalMargin; //去掉下面的分割栏
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  OrderCell *cell = (OrderCell *) [tableView dequeueReusableCellWithIdentifier:@"OrderCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderCell" owner:nil options:nil] lastObject];
  }

  cell.cellTopicData = [self.tableData objectAtIndex:indexPath.section];
  cell.orderCellType = OrderCellType_ThemeManager;
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  ThemeEditViewController *vc = [[ThemeEditViewController alloc] init];
  vc.themeEditType = ThemeEditType_Edit;
  vc.topicModel = [self.tableData objectAtIndex:indexPath.section];
  vc.delegate = self;
  [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 左滑删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  // 通过sectionfooter画分割栏，规避左滑删除高度问题。
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];

    TopicModel *topicModel = [self.tableData objectAtIndex:indexPath.section];
    [TopicModel deleteTopicWithTopicId:topicModel.topicid completion:^(BOOL isSucceed){
      if (isSucceed) {
        [self.tableData removeObjectAtIndex:indexPath.section];
        [self.tableView reloadData];
      } else {
        NSError *error = [UserModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
  }
}

#pragma mark ThemeEditVcDelegate 刷新页面
- (void)refreshThemeListVc{
  [self loadRefreshData];
}


@end
